package com.kbt.activity;


/**
 * @author zhgk
 * @package: com.kbt.activity
 * @TODO: 展示物流路径的页面Acvivity
 * @data: 2015/8/21 9:59
 * @version: V1.0
 */

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.google.gson.Gson;
import com.kbt.adapter.GoodRoadInfoAdapter;
import com.kbt.base.BaseApplication;
import com.kbt.commont.Constants;
import com.kbt.model.data.*;
import com.kbt.net.RequestManager;
import com.kbt.util.utils.LoadingDialog;
import com.kbt.util.utils.Utils;
import com.thinkland.sdk.android.DataCallBack;
import com.thinkland.sdk.android.JuheData;
import com.thinkland.sdk.android.Parameters;
import net.sf.json.JSON;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class KuaiDiActivity extends Activity {

    private ExpandableListView expandlistView;
    private GoodRoadInfoAdapter adapter;
    private List<GoodRoadInfoItem> goodRoadInfoItemList;
    private TextView textView77;//公司名
    private TextView textView79;//快递单号
    private TextView textView78;//运费
    private KuaiDiListBean kuaiDiListBean;
    private List<KuaiDiBean> kuaiDiBeans = new ArrayList<KuaiDiBean>();
    private String bm;
    private  String dh;
    private String kd_name;
    private String tv;
    private List<NewKuaiDiBean> kuaidiList =new ArrayList<NewKuaiDiBean>();
    private int code;
    private LoadingDialog mProgress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.good_road_info);
        expandlistView = (ExpandableListView) findViewById(R.id.expandlist);

        this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_return_btn);

        //设置返回按钮事件,关闭当前Activity，返回数据
        ImageView returnImg = (ImageView) findViewById(R.id.return_btn);
        returnImg.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //不创建地址
                setResult(RESULT_CANCELED, null);
                BaseApplication.getInstance().removeActivity(KuaiDiActivity.this);
                finish();
            }
        });
        BaseApplication.getInstance().addActivity(KuaiDiActivity.this);
        TextView appTitleView = (TextView) findViewById(R.id.center_title);
        appTitleView.setText("跟踪订单");
        bm = getIntent().getStringExtra("bm");
        dh = getIntent().getStringExtra("dh");
        kd_name = getIntent().getStringExtra("kd_name");
        initExpandListView();
        initData();
        BaseApplication.getInstance().addActivity(KuaiDiActivity.this);
    }

    /**
     * 初始化可拓展列表
     */
    private void initExpandListView() {
        textView77 = (TextView) findViewById(R.id.textView77);
        textView79 = (TextView) findViewById(R.id.textView79);
        textView78 = (TextView) findViewById(R.id.textView78);
        textView77.setText(kd_name+"");
        textView79.setText(dh);
        expandlistView.setGroupIndicator(null); // 去掉默认带的箭头
    }
    public void initadpater(){
        if(adapter==null) {
            adapter = new GoodRoadInfoAdapter(this, kuaidiList);
            expandlistView.setAdapter(adapter);
        }else{
            adapter.setList(kuaidiList);
            adapter.notifyDataSetChanged();
        }
    }
    //初始化数据
    private void initData(){
        showProgress();
        /**
         * 请不要添加key参数.
         */
        Parameters params = new Parameters();
        params.add("ip", "www.juhe.cn");
        params.add("dtype", "json");
        params.add("com", bm);
        params.add("no", dh);
        /**
         * 请求的方法 参数: 第一个参数 当前请求的context 第二个参数 接口id 第三二个参数 接口请求的url 第四个参数 接口请求的方式
         * 第五个参数 接口请求的参数,键值对com.thinkland.sdk.android.Parameters类型; 第六个参数
         * 请求的回调方法,com.thinkland.sdk.android.DataCallBack;
         *
         */
        JuheData.executeWithAPI(this, 43, "http://v.juhe.cn/exp/index", JuheData.GET, params, new DataCallBack() {
            /**
             * 请求成功时调用的方法 statusCode为http状态码,responseString    *为请求返回数据.
             */
            @Override
            public void onSuccess(int statusCode, String responseString) {
                // TODO Auto-generated method stub
                tv = responseString;
                code = statusCode;
            }

            /**
             * 请求完成时调用的方法,无论成功或者失败都会调用.
             */
            @Override
            public void onFinish() {
                // TODO Auto-generated method stub
                stopProgress();
                if (code == 200) {
                    JSONObject jsonObject = null;
                    JSONObject jsonObject2 = null;
                    JSONArray array = null;
                    try {
                        /**把json字符串转换成json对象**/
                        jsonObject = new JSONObject(tv);
                        String data = jsonObject.getString("result");
                        if (!data.equals("null")) {
                            JSONObject jDat = new JSONObject(data);
                            array = jDat.getJSONArray("list");
                            for (int i = array.length()-1; i >=0; i--) {
                                jsonObject2 = new JSONObject(array.getString(i));
                                String datetime = jsonObject2.getString("datetime");
                                String remark = jsonObject2.getString("remark");
                                NewKuaiDiBean newKuaiDiBean = new NewKuaiDiBean();
                                newKuaiDiBean.setDatetime(datetime);
                                newKuaiDiBean.setRemark(remark);
                                kuaidiList.add(newKuaiDiBean);
                            }
                        } else {
                            NewKuaiDiBean newKuaiDiBean = new NewKuaiDiBean();
                            newKuaiDiBean.setRemark("暂时没有物流信息!");
                            kuaidiList.add(newKuaiDiBean);

                        }
                        initadpater();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    NewKuaiDiBean newKuaiDiBean = new NewKuaiDiBean();
                    newKuaiDiBean.setRemark("暂时没有物流信息!");
                    kuaidiList.add(newKuaiDiBean);
                    initadpater();
                }

            }

            /**
             * 请求失败时调用的方法,statusCode为http状态码,throwable为捕获到的异常
             * statusCode:30002 没有检测到当前网络. 30003 没有进行初始化. 0
             * 未明异常,具体查看Throwable信息. 其他异常请参照http状态码.
             */
            @Override
            public void onFailure(int statusCode, String responseString, Throwable throwable) {
                // TODO Auto-generated method stub
                code = statusCode;
                tv = throwable.getMessage();
            }
        });
    }
    /**
     * progressDialog的展示
     */
    public void showProgress() {
        if (mProgress == null) {
            mProgress = new LoadingDialog(KuaiDiActivity.this,"");
            mProgress.setCancelable(true);
            mProgress.show();

        } else {
            if (mProgress.isShowing()) {
                stopProgress();
            } else {
                mProgress.show();
            }
        }
    }
    /**
     * progressDialog的消失
     */
    public void stopProgress() {
        if (mProgress != null) {
            mProgress.dismiss();
            mProgress = null;
        }
    }





}

